﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PagedList;

namespace VehicleOOBWeb.Models
{
    public class VehicleService
    {
        /// <summary>
        /// Lấy danh sách các vehicle theo manufacturer
        /// </summary>
        /// <param name="manufacturer_code"></param>
        /// <returns></returns>
        public static IPagedList<Vehicle> GetAllVehiclesByManufacturerCode(int manufacturer_code, int pageNumber)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                var vehicles = from p in db.Vehicles
                               where p.manufacturer_code == manufacturer_code
                               select p;
                return vehicles.ToPagedList(pageNumber, Config.PageSize);
            }
        }

        /// <summary>
        /// Lầy ra giá tiền của xe mắc nhất hiện có
        /// </summary>
        /// <param name="manufacturer_code"></param>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public static int? GetMaxPrice()
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                var maxPrice = db.Vehicles.Max(x => x.price);
                return maxPrice;
            }
        }

        /// <summary>
        /// Thêm mới một vehicle
        /// </summary>
        /// <param name="sub_type">thông tin loại object con là bus, car, loader, tractor hay truck</param>
        /// <param name="sub_vehicle">thông tin object con vehicle</param>
        /// <param name="vehicle">thông tin object vehicle</param>
        /// <returns></returns>
        public static bool AddVehicle(Vehicle vehicle, Object sub_vehicle, VehicleType sub_type)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                db.Vehicles.InsertOnSubmit(vehicle);
                db.SubmitChanges();
                switch (sub_type)
                {
                    case VehicleType.Bus:
                        Bus bus = sub_vehicle as Bus;
                        bus.bus_id = vehicle.vehicle_id;
                        db.Bus.InsertOnSubmit(bus);
                        break;
                    case VehicleType.Car:
                        Car car = sub_vehicle as Car;
                        car.car_id = vehicle.vehicle_id;
                        db.Cars.InsertOnSubmit(car);
                        break;
                    case VehicleType.Loader:
                        Loader loader = sub_vehicle as Loader;
                        loader.loader_id = vehicle.vehicle_id;
                        db.Loaders.InsertOnSubmit(loader);
                        break;
                    case VehicleType.Tractor:
                        Tractor tractor = sub_vehicle as Tractor;
                        tractor.tractor_id = vehicle.vehicle_id;
                        db.Tractors.InsertOnSubmit(tractor);
                        break;
                    case VehicleType.Truck:
                        Truck truck = sub_vehicle as Truck;
                        truck.truck_id = vehicle.vehicle_id;
                        db.Trucks.InsertOnSubmit(truck);
                        break;
                    default:
                        break;
                }
                db.SubmitChanges();
                return true;
            }
        }

        /// <summary>
        /// Cập nhật thông tin một vehicle
        /// </summary>
        /// <param name="sub_type">thông tin loại object con là bus, car, loader, tractor hay truck</param>
        /// <param name="vehicle">thông tin object vehicle</param>
        /// <returns></returns>
        public static bool UpdateVehicle(Vehicle vehicle, VehicleType sub_type)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                Vehicle m_vehicle = db.Vehicles.FirstOrDefault(v => v.vehicle_id == vehicle.vehicle_id);
                if (m_vehicle != null)
                {
                    m_vehicle.description = vehicle.description;
                    m_vehicle.image = vehicle.image;
                    m_vehicle.manufacturer_code = vehicle.manufacturer_code;
                    m_vehicle.name = vehicle.name;
                    m_vehicle.price = vehicle.price;
                    m_vehicle.quantity = vehicle.quantity;
                    m_vehicle.release_date = vehicle.release_date;
                    m_vehicle.info = vehicle.info;
                    db.SubmitChanges();
                }
                return true;
            }
        }

        /// <summary>
        /// Cập nhật thông tin một vehicle
        /// </summary>
        /// <param name="sub_type">thông tin loại object con là bus, car, loader, tractor hay truck</param>
        /// <param name="vehicle">thông tin object vehicle</param>
        /// <returns></returns>
        public static bool DeleteVehicle(int vehicle_id, VehicleType sub_type)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                Vehicle m_vehicle = db.Vehicles.FirstOrDefault(v => v.vehicle_id == vehicle_id);
                if (m_vehicle != null)
                {
                    switch (sub_type)
                    {
                        case VehicleType.Bus:
                            db.Bus.DeleteOnSubmit(m_vehicle.Bus);
                            break;
                        case VehicleType.Car:
                            db.Cars.DeleteOnSubmit(m_vehicle.Car);
                            break;
                        case VehicleType.Loader:
                            db.Loaders.DeleteOnSubmit(m_vehicle.Loader);
                            break;
                        case VehicleType.Tractor:
                            db.Tractors.DeleteOnSubmit(m_vehicle.Tractor);
                            break;
                        case VehicleType.Truck:
                            db.Trucks.DeleteOnSubmit(m_vehicle.Truck);
                            break;
                        case VehicleType.None:
                            if (m_vehicle.Bus != null)
                            {
                                db.Bus.DeleteOnSubmit(m_vehicle.Bus);
                            }
                            else if (m_vehicle.Car != null)
                            {
                                db.Cars.DeleteOnSubmit(m_vehicle.Car);
                            }
                            else if (m_vehicle.Loader != null)
                            {
                                db.Loaders.DeleteOnSubmit(m_vehicle.Loader);
                            }
                            else if (m_vehicle.Tractor != null)
                            {
                                db.Tractors.DeleteOnSubmit(m_vehicle.Tractor);
                            }
                            else if (m_vehicle.Truck != null)
                            {
                                db.Trucks.DeleteOnSubmit(m_vehicle.Truck);
                            }
                            break;
                        default:
                            break;
                    }
                    db.Vehicles.DeleteOnSubmit(m_vehicle);
                    db.SubmitChanges();
                }
                return true;
            }
        }

        /// <summary>
        /// Lấy danh sách vehicle theo loại vehicle
        /// </summary>
        /// <param name="sub_type">thông tin loại object con là bus, car, loader, tractor hay truck</param>
        /// <returns></returns>
        public static IPagedList<Vehicle> GetListVehicleByType(VehicleType sub_type, int pageIndex, int pageSize)
        {
            DataVehicleDataContext db = new DataVehicleDataContext();

            IEnumerable<Vehicle> lstResult = null;
            switch (sub_type)
            {
                case VehicleType.Bus:
                    lstResult = from b in db.Bus
                                select b.Vehicle;
                    break;
                case VehicleType.Car:
                    lstResult = from c in db.Cars
                                select c.Vehicle;
                    break;
                case VehicleType.Loader:
                    lstResult = from l in db.Loaders
                                select l.Vehicle;
                    break;
                case VehicleType.Tractor:
                    lstResult = from ta in db.Tractors
                                select ta.Vehicle;
                    break;
                case VehicleType.Truck:
                    lstResult = from tu in db.Trucks
                                select tu.Vehicle;
                    break;
                case VehicleType.None:
                    lstResult = db.Vehicles;
                    break;
                default:
                    break;
            }
            if (lstResult != null)
            {
                return lstResult.ToPagedList(pageIndex, pageSize);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy thông tin một vehicle object
        /// </summary>
        /// <param name="vehicle_id">mã vehicle</param>
        /// <param name="sub_type">thông tin loại object con là bus, car, loader, tractor hay truck</param>
        /// <returns></returns>
        public static Vehicle GetVehicleByID(int vehicle_id, VehicleType sub_type = VehicleType.None)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                Vehicle m_vehicle = db.Vehicles.FirstOrDefault(v => v.vehicle_id == vehicle_id);
                string test = m_vehicle.Manufacturer.manufacturer_name;
                if (m_vehicle != null)
                {
                    var truck = m_vehicle.Truck;
                    var tractor = m_vehicle.Tractor;
                    var bus = m_vehicle.Bus;
                    var car = m_vehicle.Car;
                    var loader = m_vehicle.Loader;

                    switch (sub_type)
                    {
                        case VehicleType.Bus:
                            vehicle_id = m_vehicle.Bus.bus_id;
                            break;
                        case VehicleType.Car:
                            vehicle_id = m_vehicle.Car.car_id;
                            break;
                        case VehicleType.Loader:
                            vehicle_id = m_vehicle.Loader.loader_id;
                            break;
                        case VehicleType.Tractor:
                            vehicle_id = m_vehicle.Tractor.tractor_id;
                            break;
                        case VehicleType.Truck:
                            vehicle_id = m_vehicle.Truck.truck_id;
                            break;
                        default:
                            break;
                    }
                    return m_vehicle;
                }
                else
                {
                    return null;
                }
            }
        }


        internal static List<Vehicle> GetHotVehicles()
        {
            DataVehicleDataContext db = new DataVehicleDataContext();
            var hotVehicles = from p in db.Vehicles
                              orderby p.price descending
                              select p;
            return hotVehicles.Take(3).ToList();
        }

        internal static List<Vehicle> GetNewVehicles()
        {
            DataVehicleDataContext db = new DataVehicleDataContext();
            var newVehicles = from p in db.Vehicles
                              orderby p.release_date descending
                              select p;
            return newVehicles.Take(3).ToList();
        }


        /// <summary>
        /// Lấy thông tin một vehicle object
        /// </summary>
        /// <param name="vehicle_id">mã vehicle</param>
        /// <param name="sub_type">thông tin loại object con là bus, car, loader, tractor hay truck</param>
        /// <returns></returns>
        public static List<Vehicle> GetListVehicleByIDs(string vehicle_ids)
        {
            DataVehicleDataContext db = new DataVehicleDataContext();

            string[] arrIds = vehicle_ids.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (arrIds != null && arrIds.Length > 0)
            {
                var lstVehicle = from v in db.Vehicles
                                 where arrIds.Contains(v.vehicle_id.ToString())
                                 select v;
                if (lstVehicle != null)
                {
                    return lstVehicle.ToList();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        internal static IPagedList<Vehicle> GetVehiclesByConditions(string vehicleType, int manufacturerCode, int priceFilter, string vehicleName, int pageNumber)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                int? maxPrice=GetMaxPrice();
                IEnumerable<Vehicle> vehicles;
                switch(vehicleType){
                    case "Car":
                        vehicles = db.Vehicles.Where(p => p.Car != null);
                        break;
                    case "Truck":
                        vehicles = db.Vehicles.Where(p => p.Truck != null);
                        break;
                    case "Bus":
                        vehicles = db.Vehicles.Where(p => p.Bus != null);
                        break;
                    case "Loader":
                        vehicles = db.Vehicles.Where(p => p.Loader != null);
                        break;
                    case "Tractor":
                        vehicles = db.Vehicles.Where(p => p.Tractor != null);
                        break;
                    default:
                        vehicles = db.Vehicles;
                        break;
                }
                
                if (manufacturerCode > 0)
                    vehicles = vehicles.Where(p => p.manufacturer_code == manufacturerCode);
                
                switch (priceFilter)
                {
                    case 1:
                        vehicles = vehicles.Where(p => p.price > (maxPrice - maxPrice / 3));
                        break;
                    case 2:
                        vehicles = vehicles.Where(p => p.price >= (maxPrice / 3)).Where(p => p.price <= (maxPrice - maxPrice / 3));
                        break;
                    case 3:
                        vehicles = vehicles.Where(p => p.price < (maxPrice / 3));
                        break;
                    default:
                        break;
                }

                if (!String.IsNullOrEmpty(vehicleName))
                    vehicles = vehicles.Where(p => p.name.Contains(vehicleName));

                return vehicles.ToPagedList(pageNumber, Config.PageSize);
            }
        }
    }


    /// <summary>
    /// Enum các loại vehicle
    /// </summary>
    public enum VehicleType
    {
        Bus, Car, Loader, Tractor, Truck, None
    }

}